package com.ivo.jabsorb;

import java.util.List;

import com.ivo.model.Category;
import com.ivo.model.EmployeeAuthority;
import com.ivo.model.Role;
import com.ivo.model.TemplateAttribute;
import com.ivo.model.UserRole;
import com.ivo.model.dcc.Document;
import com.ivo.model.dcc.DocumentVersion;
import com.ivo.service.dcc.DccService;
import com.ivo.service.pdm.CategoryService;
import com.ivo.service.pdm.RoleService;
import com.ivo.service.pdm.SheetService;
import com.ivo.service.pdm.UserRoleService;
import com.ivo.util.ApplicationContextUtil;
import com.ivo.util.Util;

public class JabsorbService extends ApplicationContextUtil implements java.io.Serializable
{
	private static final long serialVersionUID = 5254091139223592442L;
	CategoryService categoryService = (CategoryService) super.getBean( "categoryService" );
	SheetService sheetService = (SheetService) super.getBean( "sheetService" );
	RoleService roleService = (RoleService) super.getBean( "roleService" );
	UserRoleService userRoleService = (UserRoleService) getBean( "userRoleService" );
	DccService dccService = (DccService) super.getBean( "dccService" );

	public String getEditCateInfo(Long cateId)
	{
		StringBuffer strRtn = new StringBuffer();

		Category category = categoryService.getCategory( cateId );
		if( category != null ) {
			strRtn.append( "{'cate_name':'" );
			strRtn.append( Util.restoreExtStr( category.getCateName() ) );
			strRtn.append( "','cate_type':'" );
			strRtn.append( category.getCateType().getCateTypeId() );
			strRtn.append( "','sort_order':'" );
			strRtn.append( category.getSortOrder() );
			strRtn.append( "','memo':'" );
			strRtn.append( Util.restoreExtStr( category.getMemo() ) );
			strRtn.append( "','cate_id':'" );
			strRtn.append( category.getCateId() );
			strRtn.append( "'}" );
		}
		return strRtn.toString();
	}

	public String getAttributeSize(Long tmplId)
	{
		String strRtn = "";

		List<TemplateAttribute> tmplAttrs = sheetService.getTmplAttrs( tmplId );
		int sortOrder = tmplAttrs.size() + 1;
		strRtn = "{'sortOrder':'" + sortOrder + "','groupNumber':'0','tmpl_id':'" + tmplId + "','maxLength':'-1'}";

		return strRtn;
	}

	public String getEditUserRoleInfo(String roleId)
	{
		StringBuffer strRtn = new StringBuffer();

		Role role = roleService.getInstance( Role.class, roleId );
		if( role != null ) {
			List<UserRole> userRoles = userRoleService.getInstanceSet( roleId );
			String userList = "";
			for( int i = 0; i < userRoles.size(); i++ ) {
				if( i != 0 ) userList = userList + ",";
				UserRole userRole = userRoles.get( i );
				userList = userList + userRole.getUserId();
			}
			strRtn.append( "{'role_id':'" );
			strRtn.append( roleId );
			strRtn.append( "','role_name':'" );
			strRtn.append( role.getRoleName() );
			strRtn.append( "','userList':'" );
			strRtn.append( userList );
			strRtn.append( "'}" );
		}

		return strRtn.toString();
	}

	public String getUserRoleName(String userId)
	{

		String roleName = "";
		boolean bAdmin = false;

		bAdmin = roleService.bAdministrator( userId );
		if( !bAdmin ) {
			bAdmin = roleService.bProjectRole( userId, "F" );
			if( bAdmin ) {
				roleName = "project_owner";
			} else {
				bAdmin = roleService.bProjectRole( userId, "D" );
				if( bAdmin ) {
					roleName = "project_member";
				} else {
					roleName = "user";
				}
			}
		} else {
			roleName = "system";
		}

		return roleName;
	}

	public boolean bAuthDccDoc(String userId, String documentVersion_ID)
	{
		boolean bAuth = false;

		DocumentVersion objDocumentVersion = dccService.getDocumentVersion( Long.parseLong( documentVersion_ID ) );
		Document objDocument = dccService.getDocument( objDocumentVersion.getDocument_FK() );
		// bAuth = roleService.bAuthDccDoc(userId);
		bAuth = dccService.bDccSpecialPermisstions( userId, objDocument.getDocumentType_FK() );

		return bAuth;
	}

	public String getRoleUserList(String roleId, Long projectId)
	{
		String users = "";
		List<UserRole> userRoles = userRoleService.getUserRolesByCateRoleId( projectId, roleId );
		if( userRoles.size() > 0 ) {
			for( int i = 0; i < userRoles.size(); i++ ) {
				UserRole ur = userRoles.get( i );
				if( i != 0 ) users = users + ",";
				users = users + ur.getUserId();
			}
		}

		return users;
	}

	public String getCateOwnerList(Long categoryId, String type)
	{
		String users = "";

		if( type.equals( "all" ) ) {
			List<String> userRoles = null;
			userRoles = categoryService.getCategoryOwners( categoryId );
			for( int i = 0; i < userRoles.size(); i++ ) {
				String userId = userRoles.get( i );
				if( i != 0 ) users = users + ",";
				users += userId;
			}
		} else if( type.equals( "emp" ) ) {

			List<EmployeeAuthority> auths = categoryService.getEmpAuthByCate( categoryId );
			for( int i = 0; i < auths.size(); i++ ) {
				EmployeeAuthority auth = auths.get( i );

				if( i != 0 ) users = users + ",";
				users += auth.getUserId();
			}
		}

		return users;
	}

	public String getDefaultUserList(String roleId)
	{
		String users = "";
		List<UserRole> userRoles = userRoleService.getUserRoles( roleId );
		if( userRoles.size() > 0 ) {
			for( int i = 0; i < userRoles.size(); i++ ) {
				UserRole ur = userRoles.get( i );
				if( i != 0 ) users = users + ",";
				users = users + ur.getUserId();
			}
		}

		return users;
	}

	public String getProjectOwnerList(Long cateId)
	{
		String users = "";
		List<EmployeeAuthority> empAuths = categoryService.getEmpAuthByCate( cateId );
		if( empAuths.size() > 0 ) {
			for( int i = 0; i < empAuths.size(); i++ ) {
				EmployeeAuthority ea = empAuths.get( i );
				if( i != 0 ) users = users + ",";
				users = users + ea.getUserId();
			}
		}

		return users;
	}

	public boolean bAuthedReadDccDoc(String docNumber, String userId, String docStatus)
	{
		boolean bAuth = false;
		bAuth = dccService.bAuthToReadDoc( docNumber, userId, docStatus );
		return bAuth;
	}

	public String getDispatchUrl(String orderNumber, String userId, String strScreen_ID)
	{
		String url = "";
		url = Util.getUTF8HEX( "employee_ID=" + userId + "&screen_ID=" + strScreen_ID + "&trackingNumber=" + orderNumber );

		return url;
	}

	public String getDispatchDccUrl(String documentVersion_ID, String userId, String strScreen_ID)
	{
		String url = "";
		url = Util.getUTF8HEX( "employee_ID=" + userId + "&screen_ID=" + strScreen_ID + "&trackingNumber=" + "&documentVersion_ID=" + documentVersion_ID );

		return url;
	}

	public boolean hasReportType(long tmpl_id, String dataType)
	{
		boolean hasRptType = false;

		hasRptType = sheetService.hasReportType( tmpl_id, dataType );

		return hasRptType;
	}

}
